package com.hb.trade.server.project.section.service.raw.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hb.core.pojo.parent.MPCommonEntity;
import com.hb.core.pojo.parent.PageResult;
import com.hb.core.pojo.project.section.dto.ClarifyConfirmDTO;
import com.hb.core.pojo.project.section.dto.ClarifyDTO;
import com.hb.core.pojo.project.section.entity.ClarifyConfirm;
import com.hb.core.pojo.project.section.mapper.ClarifyConfirmMapper;
import com.hb.trade.server.project.section.service.raw.ClarifyConfirmService;
import org.fzzn.core.model.enums.LogicEnum;
import org.fzzn.core.util.Enums;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 澄清确认表 服务实现类
 * </p>
 *
 * @author admin
 * @since 2021-10-09
 */
@Service
public class ClarifyConfirmServiceImpl extends ServiceImpl<ClarifyConfirmMapper, ClarifyConfirm> implements ClarifyConfirmService {

    /**
     * 查询确认和未确认的总数以及总数
     *
     * @param clarifyId 澄清ID
     * @param confirmFlag 1 已确认的个数 2 未确认的个人  其他是总数
     * @return
     */
    @Override
    public int findStatusCount(Long clarifyId, Integer confirmFlag) {
        QueryWrapper<ClarifyConfirm> clarifyConfirmQueryWrapper = new QueryWrapper<>();
        clarifyConfirmQueryWrapper.lambda().eq(ClarifyConfirm::getClarifyId,clarifyId);
        //判断是不是查询 确认与未确认
        if (StrUtil.isNotEmpty(Enums.nameOf(LogicEnum.class, confirmFlag))){
            clarifyConfirmQueryWrapper.lambda().eq(ClarifyConfirm::getConfirmFlag,confirmFlag);
        }
        return (int)this.count(clarifyConfirmQueryWrapper);
    }

    @Override
    public PageResult<ClarifyConfirmDTO> pageClarifyConfirm(ClarifyDTO clarifyDTO) {
        ClarifyConfirmDTO clarifyConfirmDTO = new ClarifyConfirmDTO();
        BeanUtil.copyProperties(clarifyDTO,clarifyConfirmDTO);
        Page<ClarifyConfirmDTO> pages = clarifyConfirmDTO.pages();
        baseMapper.pageClarifyConfirm(pages,clarifyDTO.getSectionList(), clarifyDTO.getId());
        return PageResult.get(pages);
    }

    @Override
    public int getCountConfirmFlag(Long clarifyId, Long tenderId) {
        QueryWrapper<ClarifyConfirm> wrapper = ClarifyConfirm.wrapper();
        wrapper.eq("clarify_id",clarifyId)
                .eq("tender_id",tenderId);
        long count = count(wrapper);
        return (int) count;
    }
}
